<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['basic']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
/*	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在！请重新约课','error');
	}
	$order=pdo_get('jy_bdd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	*/
	$data=array();
	$data['0']=date("Y-m-d",time("day")); 
	$data['1']=date("Y-m-d",strtotime("+1 day")); 
	$data['2']=date("Y-m-d",strtotime("+2 day")); 
	$data['3']=date("Y-m-d",strtotime("+3 day")); 
	$data['4']=date("Y-m-d",strtotime("+4 day")); 
	$data['5']=date("Y-m-d",strtotime("+5 day")); 
	$data['6']=date("Y-m-d",strtotime("+6 day"));
	$lesson=pdo_get('jy_bdd_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['lessonid']));
	$store=pdo_get('jy_bdd_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['storeid']));
	$user=pdo_get('jy_bdd_user',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
//	$this->session('order_id',$order_id);
	$post=array('lessonid'=>$_RQ['lessonid'],'storeid'=>$_RQ['storeid'],'workerid'=>$_RQ['workerid'],'dayid'=>$_RQ['dayid']);
	$return['lesson']=$lesson;
	$return['day']=$data[$_RQ['dayid']];
	$return['store']=$store;
	$return['post']=$post;
	exi($return);
}
if($_RQ['op']=='notice'){
	$postdata=get_postdata();
	if(!$postdata['username']){
			exi('请输入联系人姓名','error');
		}
		if(!$postdata['mobile']){
			exi('请输入联系人电话','error');
		}
		if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  
	 
			exi('请输入正确的手机号码','error');
		} 
		$data=array();
		$data['0']=date("Y-m-d",time("day")); 
		$data['1']=date("Y-m-d",strtotime("+1 day")); 
		$data['2']=date("Y-m-d",strtotime("+2 day")); 
		$data['3']=date("Y-m-d",strtotime("+3 day")); 
		$data['4']=date("Y-m-d",strtotime("+4 day")); 
		$data['5']=date("Y-m-d",strtotime("+5 day")); 
		$data['6']=date("Y-m-d",strtotime("+6 day"));
		$price=pdo_getcolumn('jy_bdd_lesson',array('id'=>$postdata['lessonid']),'price');
		//print_r($price.'h');exit;
			//$time=ijson_encode(array('data'=>$postdata['data'],'start'=>$postdata['start'],'end'=>$postdata['end']));	
		$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'storeid'=>	$postdata['storeid'],
				'lesson_id'=>$postdata['lessonid'],
				'worker_id'=>$postdata['workerid'],
				'money'=>$price,
				'day'=>$data[$postdata['dayid']],
				'username'=>$postdata['username'],
				'mobile'=>$postdata['mobile'],
				'remark'=>$postdata['remark'],
				'ordersn'=>get_ordersn('jy_bdd_order','ordersn'),
				'createtime'=>TIMESTAMP,
			);
		pdo_insert('jy_bdd_order',$insert);
		$id=pdo_insertid();
		$this->session('order_id',$id);
		exi('','','jy_bdd/confirm/confirmpay/id='.$id);
	
}
if($_RQ['op']=='uporder'){
	$post=get_postdata();
	if(!$post){
		exi('信息错误，请重新提交','error');
	}
	$order_id=$this->session('order_id');
	$order=pdo_get('jy_bdd_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
	pdo_update('jy_bdd_order',array('status'=>'1','pay'=>0),array('id'=>$order['id']));
	exi('约课成功！','success','jy_bdd/order/list/status=1&id='.$order['id']);
}
if($_RQ['op']=='confirmpay'){
	$order_id=intval($_RQ['id']);
	$this->session('order_id',$id);
	if(!$order_id){
		exi('订单不存在！请重新约课','error');
	}
	$order=pdo_get('jy_bdd_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	$lesson=pdo_get('jy_bdd_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['lesson_id']));
	$return['lesson']=$lesson;
	$return['post']=$post;
	exi($return);
}
//支付
if($_RQ['op']=='pay'){
	$order_id = $this->session('order_id');
	$post=get_postdata();
//	if(!$post){
	//	exi('信息错误，请重新提交','error');
	//}
	//$data=array('name'=>$post['name'],'mobile'=>$post['mobile'],'address'=>$post['address']);
//	$data2=array('username'=>$post['name'],'mobile'=>$post['mobile'],'address'=>$post['address']);
//	pdo_update('jy_bdd_user',$data,array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$order=pdo_get('jy_bdd_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));	
	$params['fee']=$order['money'];
	$params['title']='约课订单';
	$params['id']=$this->session('order_id');
	$params['plugin']='jy_bdd';
	//是否关闭余额支付
	if($return['basic']['iscredit']==1){
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
	}
	$params['back_url']='jy_bdd/confirm/payresult';
	$return['payid']=cfc('pay')->create_pay_order($params);
//	print_r($return['payid'].'ff');exit;
	exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	$order=pdo_get('jy_bdd_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	pdo_update('jy_bdd_order',array('status'=>'1','pay'=>$payresult['fee']),array('id'=>$order['id']));
	exi('支付成功','success','jy_bdd/order/list/status=1&id='.$order['id']);
}